home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Misc / DXInstall / winmain.cpp < prev   
Encoding:
C/C++ Source or Header  |  2004-09-30  |  11.5 KB  |  342 lines

  1. //-----------------------------------------------------------------------------
  2. // File: WinCode.cpp
  3. //
  4. // Desc: All of the Windows specific code needed for the DSetup sample
  5. //
  6. //      The code in this file includes the main Windows entry point
  7. //      as well as code to handle messages and our modeless version of
  8. //      MessageBox().
  9. //
  10. //      Call Tree:
  11. //         WinMain                      Main Windows Entry Point
  12. //            DirectXInstallInit        Initializes & registers window class
  13. //               DirectXInstallWndProc  Processes windows messages
  14. //                  DirectXInstall      See DINSTALL.CPP
  15. //                  DirectXGetVersion   See DINSTALL.CPP
  16. //         DirectXInstall               See DINSTALL.CPP
  17. //            DlgProc                   Handles all messages for our modeless MessageBox()
  18. //               SetButtons             Initializes the text of the dialog buttons to mimmic MessageBox()
  19. //                  ShowButton          Helper function to get and set the text of a button from resource strings
  20. //
  21. // Copyright (c) Microsoft Corporation. All rights reserved.
  22. //-----------------------------------------------------------------------------
  23. #include <windows.h>
  24. #include <basetsd.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <dsetup.h>
  28. #include "resource.h"
  29. #include "DXInstall.h"
  30.  
  31.  
  32.  
  33.  
  34. //-----------------------------------------------------------------------------
  35. // Global variables
  36. //-----------------------------------------------------------------------------
  37. DWORD       g_dwStatus    = SHOW_ALL; // Filter setting for messages from DirectXSetup
  38. HINSTANCE   g_hInstance;              // Global instance handle
  39. HWND        g_hDlg        = NULL;     // Window handle to dialog proc
  40. CHAR        g_strAppTitle[256];       // Application title
  41. INT         g_iReply      = -1;       // Global value for dialog return
  42.  
  43.  
  44.  
  45.  
  46. //-----------------------------------------------------------------------------
  47. // Name: ShowButton()
  48. // Desc: Helper function to get and set the text of a button from the
  49. //       resource strings.
  50. //-----------------------------------------------------------------------------
  51. VOID ShowButton( HWND hDlg, int Id, int strid )
  52. {
  53.     HWND btnHwd = GetDlgItem( hDlg, Id );
  54.     CHAR buf[20];
  55.  
  56.     LoadString( g_hInstance, strid, buf, 20 );
  57.     SetWindowText( btnHwd, buf );
  58.     ShowWindow( btnHwd, SW_NORMAL );
  59. }
  60.  
  61.  
  62.  
  63.  
  64. //-----------------------------------------------------------------------------
  65. // Name: SetButtons()
  66. // Desc: Initializes the text of the dialog buttons to mimmic MessageBox()
  67. //-----------------------------------------------------------------------------
  68. VOID SetButtons( HWND hDlg, DWORD wMsgType )
  69. {
  70.     LONG dwStyle;
  71.  
  72.     switch( wMsgType & 0x0000000F )
  73.     {
  74.         case MB_OKCANCEL:
  75.             ShowButton( hDlg, IDBUT1, STR_OK );
  76.             ShowButton( hDlg, IDBUT2, STR_CANCEL );
  77.             ShowWindow( GetDlgItem( hDlg, IDBUT3 ), SW_HIDE );
  78.             break;
  79.  
  80.         case MB_OK:
  81.             ShowButton( hDlg, IDBUT3, STR_OK );
  82.             break;
  83.  
  84.         case MB_RETRYCANCEL:
  85.             ShowButton( hDlg, IDBUT1, STR_RETRY );
  86.             ShowButton( hDlg, IDBUT2, STR_CANCEL );
  87.             ShowWindow( GetDlgItem( hDlg, IDBUT3 ), SW_HIDE );
  88.             break;
  89.  
  90.         case MB_ABORTRETRYIGNORE:
  91.             ShowButton( hDlg, IDBUT1, STR_ABORT );
  92.             ShowButton( hDlg, IDBUT3, STR_RETRY );
  93.             ShowButton( hDlg, IDBUT2, STR_IGNORE );
  94.             break;
  95.  
  96.         case MB_YESNOCANCEL:
  97.             ShowButton( hDlg, IDBUT1, STR_YES );
  98.             ShowButton( hDlg, IDBUT3, STR_NO );
  99.             ShowButton( hDlg, IDBUT2, STR_CANCEL );
  100.             break;
  101.  
  102.         case MB_YESNO:
  103.             ShowButton( hDlg, IDBUT1, STR_YES );
  104.             ShowButton( hDlg, IDBUT2, STR_NO );
  105.             ShowWindow( GetDlgItem( hDlg, IDBUT3 ), SW_HIDE );
  106.             break;
  107.  
  108.         default:
  109.             ShowWindow( GetDlgItem( hDlg, IDBUT1 ), SW_HIDE );
  110.             ShowWindow( GetDlgItem( hDlg, IDBUT2 ), SW_HIDE );
  111.             ShowWindow( GetDlgItem( hDlg, IDBUT3 ), SW_HIDE );
  112.             break;
  113.     }
  114.  
  115.     if( !(wMsgType & MB_DEFBUTTON2) )
  116.     {
  117.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT2 ), GWL_STYLE );
  118.         SendMessage( GetDlgItem( hDlg, IDBUT2 ), BM_SETSTYLE, dwStyle & ~BS_DEFPUSHBUTTON, 0 );
  119.     }
  120.     else
  121.     {
  122.         dwStyle = GetWindowLong(GetDlgItem( hDlg, IDBUT2 ), GWL_STYLE);
  123.         SendMessage( GetDlgItem( hDlg, IDBUT2 ), BM_SETSTYLE, dwStyle | BS_DEFPUSHBUTTON, 0 );
  124.     }
  125.  
  126.     if (!(wMsgType & MB_DEFBUTTON3))
  127.     {
  128.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT3 ), GWL_STYLE);
  129.         SendMessage( GetDlgItem( hDlg, IDBUT3 ), BM_SETSTYLE, dwStyle & ~BS_DEFPUSHBUTTON, 0 );
  130.     }
  131.     else
  132.     {
  133.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT3 ), GWL_STYLE);
  134.         SendMessage( GetDlgItem( hDlg, IDBUT3 ), BM_SETSTYLE, dwStyle | BS_DEFPUSHBUTTON, 0 );
  135.     }
  136.  
  137.     if (!(wMsgType & MB_DEFBUTTON3) && !(wMsgType & MB_DEFBUTTON2))
  138.     {
  139.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT1 ), GWL_STYLE);
  140.         SendMessage( GetDlgItem( hDlg, IDBUT1 ), BM_SETSTYLE, dwStyle | BS_DEFPUSHBUTTON, 0 );
  141.     }
  142.     else
  143.     {
  144.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT1 ), GWL_STYLE );
  145.         SendMessage( GetDlgItem( hDlg, IDBUT1 ), BM_SETSTYLE, dwStyle & ~BS_DEFPUSHBUTTON, 0 );
  146.     }
  147. }
  148.  
  149.  
  150.  
  151.  
  152. //-----------------------------------------------------------------------------
  153. // Name: DlgProc()
  154. // Desc: Message proc for our modeless version of MessageBox()
  155. //       This function sets g_wReply for GetReply()
  156. //-----------------------------------------------------------------------------
  157. DLGPROC DlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  158. {
  159.     switch( msg )
  160.     {
  161.         case WM_INITDIALOG:
  162.             SetButtons( hDlg, 0xffffffff );
  163.             break;
  164.  
  165.         case WM_COMMAND:
  166.             switch( LOWORD(wParam) )
  167.             {
  168.                 case IDBUT1:
  169.                 case IDBUT2:
  170.                 case IDBUT3:
  171.                     // Let GetReply() know the user clicked on a button
  172.                     g_iReply = LOWORD(wParam);
  173.                     break;
  174.             }
  175.             break;
  176.         case WM_ACTIVATE:
  177.             if( LOWORD(wParam) == WA_INACTIVE )
  178.             {
  179.                 if( (HWND)lParam == GetParent( hDlg ) )
  180.                 {
  181.                     SetForegroundWindow( hDlg );
  182.                 }
  183.             }
  184.             break;
  185.     }
  186.  
  187.     return 0;
  188. }
  189.  
  190.  
  191.  
  192.  
  193. //-----------------------------------------------------------------------------
  194. // Name: SetStatusChecks()
  195. // Desc: Helper function to set checkmarks by status menu items
  196. //-----------------------------------------------------------------------------
  197. VOID SetStatusChecks( HWND hWnd )
  198. {
  199.     CheckMenuItem( GetMenu( hWnd ), IDSHOWALL,      MF_BYCOMMAND|MF_UNCHECKED );
  200.     CheckMenuItem( GetMenu( hWnd ), IDSHOWUPGRADES, MF_BYCOMMAND|MF_UNCHECKED );
  201.     CheckMenuItem( GetMenu( hWnd ), IDSHOWPROBLEMS, MF_BYCOMMAND|MF_UNCHECKED );
  202.     CheckMenuItem( GetMenu( hWnd ), IDSHOWNOTHING,  MF_BYCOMMAND|MF_UNCHECKED );
  203.  
  204.     switch( g_dwStatus )
  205.     {
  206.         case SHOW_ALL:
  207.             CheckMenuItem( GetMenu( hWnd ), IDSHOWALL, MF_BYCOMMAND|MF_CHECKED );
  208.             break;
  209.         case SHOW_UPGRADES:
  210.             CheckMenuItem( GetMenu( hWnd ), IDSHOWUPGRADES, MF_BYCOMMAND|MF_CHECKED );
  211.             break;
  212.         case SHOW_PROBLEMS:
  213.             CheckMenuItem( GetMenu( hWnd ), IDSHOWPROBLEMS, MF_BYCOMMAND|MF_CHECKED );
  214.             break;
  215.         case SHOW_NONE:
  216.             CheckMenuItem( GetMenu( hWnd ), IDSHOWNOTHING, MF_BYCOMMAND|MF_CHECKED );
  217.             break;
  218.     }
  219. }
  220.  
  221.  
  222.  
  223.  
  224. //-----------------------------------------------------------------------------
  225. // Name: DirectXInstallWndProc()
  226. // Desc: Processes windows messages
  227. //-----------------------------------------------------------------------------
  228. INT_PTR CALLBACK DirectXInstallWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  229. {
  230.     switch( msg )
  231.     {
  232.         case WM_COMMAND:
  233.             // Process menu items
  234.             switch( LOWORD(wParam) )
  235.             {
  236.                 case IDINSTALL:
  237.                     DirectXInstall( hWnd );
  238.                     break;
  239.                 case IDGETVERSION:
  240.                     DirectXGetVersion( hWnd );
  241.                     break;
  242.                 case IDEXIT:
  243.                     DestroyWindow( hWnd );
  244.                     break;
  245.                 case IDSHOWALL:
  246.                     g_dwStatus = SHOW_ALL;
  247.                     SetStatusChecks( hWnd );
  248.                     break;
  249.                 case IDSHOWUPGRADES:
  250.                     g_dwStatus = SHOW_UPGRADES;
  251.                     SetStatusChecks( hWnd );
  252.                     break;
  253.                 case IDSHOWPROBLEMS:
  254.                     g_dwStatus = SHOW_PROBLEMS;
  255.                     SetStatusChecks( hWnd );
  256.                     break;
  257.                 case IDSHOWNOTHING:
  258.                     g_dwStatus = SHOW_NONE;
  259.                     SetStatusChecks( hWnd );
  260.                     break;
  261.             }
  262.             return 0;
  263.  
  264.         case WM_DESTROY:
  265.             PostQuitMessage( 0 );
  266.             return 0;
  267.     }
  268.  
  269.     return DefWindowProc( hWnd, msg, wParam, lParam );
  270. }
  271.  
  272.  
  273.  
  274.  
  275. //-----------------------------------------------------------------------------
  276. // Name: DirectXInstallInit()
  277. // Desc: Initializes window data and registers window class
  278. //       Sets up a structure to register the window class.  Structure includes
  279. //       such information as what function will process messages, what cursor
  280. //       and icon to use, etc.
  281. //-----------------------------------------------------------------------------
  282. BOOL DirectXInstallInit( HINSTANCE hInstance )
  283. {
  284.     WNDCLASS wndClass;      // structure pointer
  285.     ZeroMemory( &wndClass, sizeof(WNDCLASS) );
  286.     wndClass.style         = CS_GLOBALCLASS;
  287.     wndClass.lpfnWndProc   = (WNDPROC) DirectXInstallWndProc;
  288.     wndClass.hInstance     = hInstance;
  289.     wndClass.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MAIN_ICON));
  290.     wndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  291.     wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  292.     wndClass.lpszMenuName  = "MainMenu";
  293.     wndClass.lpszClassName = (LPSTR) "DirectXInstall";
  294.  
  295.     // Returns result of registering the window
  296.     return RegisterClass( &wndClass );
  297. }
  298.  
  299.  
  300.  
  301.  
  302. //-----------------------------------------------------------------------------
  303. // Name: WinMain()
  304. // Desc: Calls initialization function, processes message loop
  305. //       This will initialize the window class if it is the first time this
  306. //       application is run.  It then creates the window, and processes the
  307. //       message loop until a PostQuitMessage is received.  It exits the
  308. //       application by returning the value passed by the PostQuitMessage.
  309. //-----------------------------------------------------------------------------
  310. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  311.                     LPSTR strCmdLine, int nCmdShow )
  312. {
  313.     // Has application been initialized?
  314.     if( NULL == hPrevInstance )
  315.         if( 0 == DirectXInstallInit( hInstance ) )
  316.             return 0;
  317.  
  318.     g_hInstance = hInstance;
  319.  
  320.     HWND hWnd = CreateWindow( "DirectXInstall", "DirectX Install",
  321.                               WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
  322.                               CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  323.                               NULL, NULL, g_hInstance, NULL );
  324.     if( NULL == hWnd )
  325.         return 0;
  326.  
  327.     LoadString( g_hInstance, STR_TITLE, g_strAppTitle, 200 );
  328.     ShowWindow( hWnd, SW_NORMAL );
  329.     UpdateWindow( hWnd );     // Send a WM_PAINT message
  330.     SetStatusChecks( hWnd );  // Check the default message menu item
  331.  
  332.     MSG msg;
  333.     while( GetMessage( &msg, NULL, 0, 0 ) )
  334.     {
  335.         TranslateMessage( &msg );
  336.         DispatchMessage( &msg );
  337.     }
  338.  
  339.     // Returns the value from PostQuitMessage
  340.     return (int)msg.wParam;    
  341. }
  342.